local hx__chuanshi = fk.CreateSkill {
  name = "hx__chuanshi",
  tags = { Skill.Compulsory },
}

local U = require "packages.utility.utility"

local function hx__chuanshiChange(player, hp, skill_name)
  local room = player.room
  local skills = player.tag[hx__chuanshi.name] or {}
  if player.hp == hp then
    if not player:hasSkill(skill_name, true) then
      player:broadcastSkillInvoke(hx__chuanshi.name)
      room:handleAddLoseSkills(player, skill_name, nil, false, true)
      table.insert(skills, skill_name)
    end
  else
    if player:hasSkill(skill_name, true) then
      room:handleAddLoseSkills(player, "-"..skill_name, nil, false, true)
      table.removeOne(skills, skill_name)
    end
  end
  player.tag[hx__chuanshi.name] = skills
end

local spec = {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__chuanshi.name, true) and player.hp <= 4
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, hx__chuanshi.name, "special")
    player:broadcastSkillInvoke(hx__chuanshi.name)
    hx__chuanshiChange(player, 1, "ex__yiji")
    hx__chuanshiChange(player, 2, "ol_ex__jieming")
    hx__chuanshiChange(player, 3, "chengxiang")
    hx__chuanshiChange(player, 4, "ofl_shiji__huishi")
  end,
}


hx__chuanshi:addAcquireEffect(function(self, player, is_start)
  player.room:notifySkillInvoked(player, hx__chuanshi.name, "special")
  hx__chuanshiChange(player, 1, "ex__yiji")
  hx__chuanshiChange(player, 2, "ol_ex__jieming")
  hx__chuanshiChange(player, 3, "chengxiang")
  hx__chuanshiChange(player, 4, "ofl_shiji__huishi")
end)

hx__chuanshi:addEffect(fk.HpChanged, spec)
hx__chuanshi:addEffect(fk.MaxHpChanged, spec)
hx__chuanshi:addEffect(fk.GameStart, spec)
hx__chuanshi:addEffect(U.GeneralAppeared, spec)

hx__chuanshi:addEffect(fk.GameStart, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill("ofl_shiji__huishi", true)
  end,
  on_use = function (self, event, target, player, data)
    player.room:changeMaxHp(player, -1)
  end,
})


hx__chuanshi:addEffect(fk.EventLoseSkill, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and data.skill.name == "hx__chuanshi" and #(player.tag[hx__chuanshi.name] or {}) > 0
  end,
  on_use = function (self, event, target, player, data)
    local skills = player.tag[hx__chuanshi.name]
    player.room:handleAddLoseSkills(player, "-"..table.concat(skills, "|"), nil, false, true)
    player.tag[hx__chuanshi.name] = nil
  end,
})

return hx__chuanshi
